package java_thinking.unit_18_IO._18_1;

import java_thinking.net.mindview.util.PPrint;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Desc:   目录实用工具
 *           .* 就是单个字符匹配任意次，即贪婪匹配
 *           .*? 是满足条件的情况只匹配一次，即最小匹配
 * @author: cww
 * @DateTime: 2019-05-12 15:04
 */

public class Directory {
    public static void main(String[] args) {
//        String[] arg = {".\\cache"};
//
//        for (String s : arg) {
//            System.out.println(walk(s));
//        }
//        File[] file = local(arg[0],".txt");
//        for (File file1 : file) {
//            System.out.println(file1.getName());
//        }
        String dir = "src/maindemo/java/java_thinking/unit_18_IO";
        TreeInfo treeInfo = Directory.walk(dir, ".*\\.java");
        System.out.println(treeInfo);

    }
    public static File[] local(File dir, final String regex) {
        return dir.listFiles(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(regex);
            @Override
            public boolean accept(File dir, String name) {
                return pattern.matcher(new File(name).getName()).matches();
            }
        });
    }
    public static File[] local(String path, final String regex) {
        return local(new File(path), regex);
    }

    /**
     * 收集并打印所有目录与文件夹
     */
    public static class TreeInfo implements Iterable<File> {
        public List<File> files = new ArrayList<>();
        public List<File> dirs = new ArrayList<>();
        @Override
        public Iterator<File> iterator() {
            return files.iterator();
        }
        void addAll(TreeInfo other) {
            files.addAll(other.files);
            dirs.addAll(other.dirs);
        }
        @Override
        public String toString() {
            return "dirs: " + PPrint.pformat(dirs) + "\n\nfiles: " + PPrint.pformat(files);
        }
    }
    public static TreeInfo walk(String start, String regex) {
        return recurseDirs(new File(start), regex);
    }
    public static TreeInfo walk(File start, String regex) {
        return recurseDirs(start, regex);
    }
    public static TreeInfo walk(File start) {
        return recurseDirs(start, ".*");
    }
    public static TreeInfo walk(String start) {
        return recurseDirs(new File(start), ".*\\.class");
    }

    /**
     * 递归列出所有文件夹，和满足正则的文件
     * @param startDir
     * @param regex
     * @return
     */
    static TreeInfo recurseDirs(File startDir, String regex) {
        TreeInfo result = new TreeInfo();
        for (File item : startDir.listFiles()) {
            //是否是文件夹
            if (item.isDirectory()) {
                result.dirs.add(item);
                result.addAll(recurseDirs(item, regex));
            } else {
                if (item.getName().matches(regex)) {
                    result.files.add(item);
                }
            }
        }
        return result;
    }
}
